<?php
/**
 * A very light framework built on webappkit kits structure
 * base class for controllers
 * This file is part of Webappkit, a packages system for PHP web apps
 * @link http://webappkit.net
 * @package webappkit
 * @subpackage engine
 * @author J.Ducastel <nospam0@ducastel.name>
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 */
class WakEngineController {

    /**
     * constructor
     * @param Webappkit $kit parent kit
     */
    public function __construct(&$kit) {
        // class = {name}_Controller
        $this->name=substr(get_class($this),0,-11);
        $this->kit=&$kit;
    }/**/

    /**
     * tries to serve response
     * @access public
     * @param WakEngineResponse $response
     */
    final public function run(&$response) {
        // importing response
        $this->response=&$response;
        $this->request=$response->request;
        // pre-action stuff
        $this->_setUp();
        // searching action to perform, if not defined
        $this->_setAction($response->shiftNode());
        // performing requested action
        $result=$this->_runActionMethod($response);
        // post-action stuff
        $this->_tearDown();
        return $result;
        //} catch (Exception $e) {
        //return false;
        // }
    }

    /**
     * implement this method to perform pre-action stuff
     * @access protected
     */
    protected function _setUp() {

    }

    /**
     * implement this method to perform post-action stuff
     */
    protected function _tearDown() {

    }

    /**
     * define action to perform and response's view
     * @param string $action default to 'index'
     * @return bool
     */
    protected function _setAction($action='index') {
        if ($this->action)
            return false;
        $this->action=$action; // echo "<p>action = $action</p>";
        $this->response->view = strtolower($this->name).'.'.$action;
        return true;
    }

    /**
     * tells if request's method is POST
     * @return bool
     */
    protected function _isPost() {
        return $this->request->isPost();
    }

    /**
     * performs requested action
     * @access private
     * @param string $method
     * @param WakEngineResponse $response
     * @return mixed
     */
    private function _runActionMethod(&$response) {
        // checkign that action method is implemented
        $method=$this->action;
        if (!method_exists($this,$method))
            throw new WakEngineErrorPage("controller does not implement requested method ($method)",500);
        // getting remaining nodes
        $nodes=$response->getNextNodes();
        switch (count($nodes)) {
            case 0:
                return $this->$method();
            case 1:
                return $this->$method($nodes[0]);
            case 2:
                return $this->$method($nodes[0],$nodes[1]);
            case 3:
                return $this->$method($nodes[0],$nodes[1],$nodes[2]);
            case 4:
                return $this->$method($nodes[0],$nodes[1],$nodes[2],$nodes[3]);
            default:
                return false;
        }
    }

    /**
    * @var string the action method name that is or will be called
    */
    protected $action;

    /**
     * @var Webappkit parent kit
     */
    protected $kit;
}
